#!/bin/bash

# Nginx 网关启动脚本
# Nginx Gateway Startup Script

set -e

echo "🌐 启动 Nginx 网关..."
echo "================================"

# 检查 Docker 和 Docker Compose
if ! command -v docker &> /dev/null; then
    echo "❌ Docker 未安装，请先安装 Docker"
    exit 1
fi

if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo "❌ Docker Compose 未安装，请先安装 Docker Compose"
    exit 1
fi

# 进入项目根目录
cd "$(dirname "$0")/.."

echo "📁 当前目录: $(pwd)"

# 检查 docker-compose.yml 是否存在
if [ ! -f "docker-compose.yml" ]; then
    echo "❌ docker-compose.yml 不存在"
    exit 1
fi

echo "🚀 启动 Nginx 网关..."

# 使用新版本的 docker compose 或旧版本的 docker-compose
if docker compose version &> /dev/null; then
    COMPOSE_CMD="docker compose"
else
    COMPOSE_CMD="docker-compose"
fi

# 只启动 Nginx 服务
$COMPOSE_CMD up -d nginx

echo "⏳ 等待 Nginx 服务启动..."
sleep 5

# 检查服务状态
echo "📊 检查服务状态..."
$COMPOSE_CMD ps nginx

echo "✅ Nginx 网关启动完成！"
echo "🌐 HTTP: http://localhost:80"
echo "🔒 HTTPS: https://localhost:443"
echo ""
echo "🛑 停止服务: $COMPOSE_CMD down nginx"